﻿using System.Data.SQLite;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace Sec_scan指纹识别系统
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            StartPosition = FormStartPosition.CenterScreen;
            Control.CheckForIllegalCrossThreadCalls = false;
        }

        private void 退出ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            //清空
            listView1.Items.Clear();
            listView2.Items.Clear();
            richTextBox1.Clear();
            MessageBox.Show("数据已清空！");
        }
        public Thread th;
        public Thread th1;
        public string paths;//特征路径
        public string key;//关键特征
        public string userna;//系统名称
        public string sversion;
        public string stype;
        private void button1_Click(object sender, EventArgs e)
        {
            listView1.Items.Clear();
            listView2.Items.Clear();
            richTextBox1.Clear();
            //指纹识别
            th = new Thread(WebScanInfos);
            th.Start();
        }
        /// <summary>
        /// 指纹识别
        /// </summary>
        public void WebScanInfos()
        {
            string sqlinfo = "data source=Sec_infoscan.db";
            SQLiteConnection dataConn = new SQLiteConnection(sqlinfo);
            dataConn.Open();
            try
            {
                if (dataConn.State == ConnectionState.Open)
                {
                    string sql = "SELECT ID, fingepath, fingekey, fingename, fingeip, systemname, systemversion, systemtype FROM Sec_form";
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    using (SQLiteDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dr.Read())
                        {
                            string s1 = dr["fingepath"].ToString();
                            string s2 = dr["fingekey"].ToString();
                            string s3 = dr["systemname"].ToString();
                            string s4 = dr["systemversion"].ToString();
                            string s5 = dr["systemtype"].ToString();
                            paths = s1;
                            key = s2;
                            userna = s3;
                            sversion = s4;
                            stype = s5;
                            ScanInfos();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("数据库无法打开");
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        public string s111;
        public string s112;
        public string s113;
        public string s114;
        public string s115;
        /// <summary>
        /// 扫描WAF数据库
        /// </summary>
        public void Stunfscan()
        {
            string sqlinfo = "data source=Sec_infoscan.db";
            SQLiteConnection dataConn = new SQLiteConnection(sqlinfo);
            dataConn.Open();
            try
            {
                if (dataConn.State == ConnectionState.Open)
                {
                    string sql = "SELECT ID, fingewafname, fingewafkey, fingewafform, Serverinfo, PoweredBy FROM Sec_foms";
                    SQLiteCommand command = new SQLiteCommand(sql, dataConn);
                    using (SQLiteDataReader dr = command.ExecuteReader(CommandBehavior.CloseConnection))
                    {
                        while (dr.Read())
                        {
                            string s1 = dr["fingewafname"].ToString();
                            string s2 = dr["fingewafkey"].ToString();
                            string s3 = dr["fingewafform"].ToString();
                            string s4 = dr["Serverinfo"].ToString();
                            string s5 = dr["PoweredBy"].ToString();
                            s111 = s1;
                            s112 = s2;
                            s113 = s3;
                            s114 = s4;
                            s115 = s5;
                            ScanInfos1();
                        }
                    }
                }
                else
                {
                    MessageBox.Show("数据库无法打开");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
        /// <summary>
        /// 执行扫描
        /// </summary>
        public void ScanInfos()
        {
            string url = textBox1.Text;
            try
            {
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url + paths);
                httpReq.Method = "GET";//获取请求方式
                httpReq.Accept = "*/*";
                httpReq.ServicePoint.Expect100Continue = false;//加快载入速度
                httpReq.ServicePoint.UseNagleAlgorithm = false;//禁止nagle算法加快载入速度
                httpReq.AllowWriteStreamBuffering = false;//禁止缓冲加快载入速度
                httpReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");//定义gzip压缩页面支持
                httpReq.ContentType = "application/x-www-form-urlencoded";//定义文档类型及编码
                httpReq.AllowAutoRedirect = false;//禁止自动跳转
                httpReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
                httpReq.Timeout = 2000;//请求超时
                httpReq.KeepAlive = true;//启用长连接
                httpReq.ServicePoint.ConnectionLimit = int.MaxValue;//定义最大连接数
                HttpWebResponse response = (HttpWebResponse)httpReq.GetResponse();
                Stream receiveStream = response.GetResponseStream();
                StreamReader readStream = new StreamReader(receiveStream, Encoding.GetEncoding("UTF-8"));
                string cc = response.GetResponseHeader("Server");
                string c1 = response.GetResponseHeader("X-Powered-By");
                string html = readStream.ReadToEnd();
                string sload = html;//获取源代码
                for (int i = 0; i < 1; i++)
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(sload, key))//key是关键特征
                    {
                        if (html.Length >= 1)
                        {
                            ListViewItem item = new ListViewItem();
                            item.Text = "1";
                            item.SubItems.Add(cc);
                            item.SubItems.Add("null");
                            item.SubItems.Add(userna);
                            item.SubItems.Add(sversion);
                            item.SubItems.Add(stype);
                            listView1.Items.Add(item);
                            this.listView1.Items[this.listView1.Items.Count - 1].EnsureVisible();
                            return;
                            //MessageBox.Show("存在关键特征：" + key);
                        }
                    }
                    else
                    {
                        Console.WriteLine(richTextBox1.Text += "创建时间：" + DateTime.Now + "识别路径：" + url + paths + "  ---  " + "无法识别WEB容器" + "\r\n");
                        //ZscanInfo();
                    }
                }
            }
            catch(Exception)
            {
                //MessageBox.Show("123");
                Console.WriteLine(richTextBox1.Text += "创建时间：" + DateTime.Now + "识别路径：" + url + paths + "  ---  " + "无法识别WEB容器" + "\r\n");
            }
            //识别waf
            th1 = new Thread(Stunfscan);
            th1.Start();
        }
        public void ScanInfos1()
        {
            string url = textBox1.Text;
            try
            {
                HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url + paths);
                httpReq.Method = "GET";//获取请求方式
                httpReq.Accept = "*/*";
                httpReq.ServicePoint.Expect100Continue = false;//加快载入速度
                httpReq.ServicePoint.UseNagleAlgorithm = false;//禁止nagle算法加快载入速度
                httpReq.AllowWriteStreamBuffering = false;//禁止缓冲加快载入速度
                httpReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");//定义gzip压缩页面支持
                httpReq.ContentType = "application/x-www-form-urlencoded";//定义文档类型及编码
                httpReq.AllowAutoRedirect = false;//禁止自动跳转
                httpReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
                httpReq.Timeout = 2000;//请求超时
                httpReq.KeepAlive = true;//启用长连接
                httpReq.ServicePoint.ConnectionLimit = int.MaxValue;//定义最大连接数
                HttpWebResponse reponse = (HttpWebResponse)httpReq.GetResponse();
                string cc = reponse.GetResponseHeader("Server");
                //string c1 = reponse.GetResponseHeader("X-Powered-By");
                //string sload = c1;//获取源代码
                //if (sload.Length <= 0)
                //{
                //    richTextBox1.Text += "创建时间：" + DateTime.Now + "识别路径：" + url + paths + "  ---  " + "无法识别WAF" + "\r\n";
                //}
                for (int i = 0; i < 1; i++)
                {
                    if (System.Text.RegularExpressions.Regex.IsMatch(cc, s114))//key是关键特征
                    {
                        if (cc.Length >= 1)
                        {
                            ListViewItem item = new ListViewItem();
                            item.Text = s111;
                            item.SubItems.Add(s113);
                            listView2.Items.Add(item);
                            this.listView2.Items[this.listView2.Items.Count - 1].EnsureVisible();//自动滚动显示最后一行
                            return;
                            //MessageBox.Show("存在关键特征：" + key);
                        }
                    }
                }  
            }
            catch (Exception)
            {
                richTextBox1.Text += "创建时间：" + DateTime.Now + "识别路径：" + url + paths + "  ---  " + "无法识别WAF" + "\r\n";
            }
        }

        private void 指纹管理ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //指纹管理
            Form2 so = new Form2();
            so.StartPosition = FormStartPosition.CenterScreen;
            so.ShowDialog();
        }

        /// <summary>
        /// 中间件信息扫描
        /// </summary>
        //public void ZscanInfo()
        //{
        //    string url = textBox1.Text;
        //    try
        //    {
        //        HttpWebRequest httpReq = (HttpWebRequest)WebRequest.Create(url + paths);
        //        httpReq.Method = "GET";//获取请求方式
        //        httpReq.Accept = "*/*";
        //        httpReq.ServicePoint.Expect100Continue = false;//加快载入速度
        //        httpReq.ServicePoint.UseNagleAlgorithm = false;//禁止nagle算法加快载入速度
        //        httpReq.AllowWriteStreamBuffering = false;//禁止缓冲加快载入速度
        //        httpReq.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");//定义gzip压缩页面支持
        //        httpReq.ContentType = "application/x-www-form-urlencoded";//定义文档类型及编码
        //        httpReq.AllowAutoRedirect = false;//禁止自动跳转
        //        httpReq.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/50.0.2661.102 Safari/537.36";
        //        httpReq.Timeout = 2000;//请求超时
        //        httpReq.KeepAlive = true;//启用长连接
        //        httpReq.ServicePoint.ConnectionLimit = int.MaxValue;//定义最大连接数
        //        HttpWebResponse reponse = (HttpWebResponse)httpReq.GetResponse();
        //        string cc = reponse.GetResponseHeader("Server");
        //        string c1 = reponse.GetResponseHeader("X-Powered-By");
        //        ListViewItem item = new ListViewItem();
        //        item.Text = "1";
        //        item.SubItems.Add(cc);
        //        item.SubItems.Add("null");
        //        item.SubItems.Add(userna);
        //        item.SubItems.Add(sversion);
        //        item.SubItems.Add(stype);
        //        listView1.Items.Add(item);
        //        this.listView1.Items[this.listView1.Items.Count - 1].EnsureVisible();//自动滚动显示最后一行
        //        //th1 = new Thread(Stunfscan);
        //        //th1.Start();
        //        return;
        //        //for (int i = 0; i < 1; i++)
        //        //{
        //        //    if (System.Text.RegularExpressions.Regex.IsMatch(c1, s115))//key是关键特征
        //        //    {
        //        //        if (c1.Length >= 1)
        //        //        {
        //        //            ListViewItem item = new ListViewItem();
        //        //            item.Text = "1";
        //        //            item.SubItems.Add(cc);
        //        //            item.SubItems.Add("null");
        //        //            item.SubItems.Add(userna);
        //        //            item.SubItems.Add(sversion);
        //        //            item.SubItems.Add(stype);
        //        //            listView1.Items.Add(item);
        //        //            this.listView1.Items[this.listView1.Items.Count - 1].EnsureVisible();//自动滚动显示最后一行
        //        //        }
        //        //    }
        //        //}
        //    }
        //    catch(Exception)
        //    {
        //        richTextBox1.Text += "创建时间：" + DateTime.Now + "识别路径：" + url + paths + "  ---  " + "无法识别WEB容器" + "\r\n";
        //    }
        //    //识别waf
        //    //th1 = new Thread(Stunfscan);
        //    //th1.Start();
        //}
    }
}
